package com.turkcell.dv.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.turkcell.dv.dao.BusinessProcessDAO;
import com.turkcell.dv.dao.PrivilegeDAO;
import com.turkcell.dv.dao.ProcessEndpointDAO;
import com.turkcell.dv.dao.ProcessPrivilegeDAO;
import com.turkcell.dv.dao.UserDAO;
import com.turkcell.dv.dao.UserPrivilegeDAO;
import com.turkcell.dv.model.BusinessProcessDOM;
import com.turkcell.dv.model.PrivilegeDOM;
import com.turkcell.dv.model.ProcessEndpointDOM;
import com.turkcell.dv.model.ProcessPrivilegeDOM;
import com.turkcell.dv.model.UserDOM;
import com.turkcell.dv.model.UserPrivilegeDOM;

@SuppressWarnings("unused")
@Service("identityManagementService")
@Transactional(readOnly = true)
public class IdentityManagementServiceImpl implements IdentityManagementService {

	@Autowired
	private UserDAO userDAO;
	@Autowired
	private UserPrivilegeDAO userPrivilegeDAO;
	@Autowired
	private BusinessProcessDAO businessProcessDAO;
	@Autowired
	private ProcessEndpointDAO businessProcessEndpointDAO;
	@Autowired
	private PrivilegeDAO privilegeDAO;
	@Autowired
	private ProcessPrivilegeDAO processPrivilegeDAO;

	public void setPersonDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void setBusinessProcessDAO(BusinessProcessDAO businessProcessDAO) {
		this.businessProcessDAO = businessProcessDAO;
	}

	public void setPrivilegeDAO(PrivilegeDAO privilegeDAO) {
		this.privilegeDAO = privilegeDAO;
	}

	@Override
	@Transactional
	public UserDOM addUser(UserDOM u) {
		this.userDAO.addUser(u);
		return u;
	}

	@Override
	@Transactional
	public UserDOM updateUser(UserDOM u) {
		this.userDAO.updateUser(u);
		return u;
	}

	@Override
	@Transactional
	public List<UserDOM> getUserList() {
		return this.userDAO.listUsers();
	}

	@Override
	@Transactional
	public UserDOM getUserById(long id) {
		return this.userDAO.getUserById(id);
	}
	@Override
	@Transactional
	public UserDOM getUserByUsername(String username) {
		return this.userDAO.getUserByUsername(username);
	}

	@Override
	@Transactional
	public void removeUser(long id) {
		this.userDAO.removeUser(id);
	}

	@Override
	@Transactional
	public boolean checkUserAuthorization(String username, String endPointURL) {
		boolean authorized = false;
		UserDOM u = userDAO.getUserByUsername(username);
		List<UserPrivilegeDOM> upList = userPrivilegeDAO.listUserPrivilegesByUserId(u.getId());
		List<ProcessPrivilegeDOM> ppList = processPrivilegeDAO.listProcessPrivileges();
		List<PrivilegeDOM> pList = privilegeDAO.listPrivileges();

		for (int i = 0; i < pList.size(); i++) {
			boolean exist = false;
			for (int j = 0; j < upList.size(); j++) {

				if (upList.get(j).getPrivilegeId() == pList.get(i).getId()) {
					exist = true;
				}
				if (exist)
					break;
			}
			if (!exist) {
				pList.remove(i);
				i--;
			}
		}
		// At this point pList has the privileges of User

		for (int i = 0; i < pList.size(); i++) {
			for (int j = 0; j < ppList.size(); j++) {
				if (pList.get(i).getId() == ppList.get(j).getPrivilegeId()) {
					List<ProcessEndpointDOM> enpoints = businessProcessEndpointDAO.listEndpointsByProcessId(ppList.get(j).getProcessId());
					for (int k = 0; k < enpoints.size(); k++) {
						if (enpoints.get(k).getEndpoint().equals(endPointURL)) {
							authorized = true;
							break;
						}
					}
				}
				if (authorized == true)
					break;
			}
			if (authorized == true)
				break;
		}

		return authorized;
	}

	@Override
	@Transactional
	public Map<String, Object> getUserDetails(String username) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		UserDOM u = userDAO.getUserByUsername(username);

		resultMap.put("UserDOM", u);

		List<UserPrivilegeDOM> upList = userPrivilegeDAO.listUserPrivilegesByUserId(u.getId());
		List<PrivilegeDOM> pList = privilegeDAO.listPrivileges();
		for (int i = 0; i < pList.size(); i++) {
			boolean found = false;
			for (int j = 0; j < upList.size(); j++) {
				if (pList.get(i).getId() == upList.get(j).getPrivilegeId()) {
					found = true;
				}
			}
			if (!found) {
				pList.remove(i);
				i--;
			}
		}
		resultMap.put("privilegeList", pList);
		return resultMap;
	}
}
